//
//  BNDODAssociatedView.m
//  OfficeDoc
//
//  Created by pie on 2022/4/3.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDODAssociatedView.h"
#import "BNDOfficeDocPDFView.h"
#import "LVUIUtils.h"
#import "LVHUD.h"
#import "BNDPushVCHandler.h"

@interface BNDODAssociatedView ()
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) NSMutableArray *pdfListData;
@property (assign, nonatomic) NSInteger index;
@end

@implementation BNDODAssociatedView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [LVUIUtils addLineInView:self top:NO leftMargin:16 rightMargin:16];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = BNDPFRegularFont(14);
        _titleLabel.textColor = UIColorWithHex(0x51555D);
        _titleLabel.text = @"关联公文：";
        _titleLabel.numberOfLines = 1;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.top.mas_equalTo(self.mas_top).offset(8);
        }];
        
        
    }
    
    return self;
}

- (void)setInfo:(BNDFileInfo *)info
{
    _info = info;
    
    if(info.fileJsonArray.count  ==0){
        UIView *lineView = [[UIView alloc] init];
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(0);
            make.right.mas_equalTo(self.mas_right).offset(0);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(0);
            make.height.mas_equalTo(16);
            make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        }];
    }else{
        @weakify(self)
        [info.fileJsonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            @strongify(self)
            BNDOfficeDocPDFView *pdfView = [[BNDOfficeDocPDFView alloc] init];
            pdfView.model = self.model;
            pdfView.infoData = obj;
            pdfView.tag = 100+ idx;
            [self addSubview:pdfView];
            [self.pdfListData addObject:pdfView];
            if (idx==info.fileJsonArray.count-1) {
                [pdfView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(self.mas_left).offset(16);
                    make.right.mas_equalTo(self.mas_right).offset(-16);
                    make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(16*(idx+1)+403*idx);
                    make.height.mas_equalTo(403);
                    make.bottom.mas_equalTo(self.mas_bottom).offset(-16);
                }];
            }else{
                [pdfView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(self.mas_left).offset(16);
                    make.right.mas_equalTo(self.mas_right).offset(-16);
                    make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(16*(idx+1)+403*idx);
                    make.height.mas_equalTo(403);
                }];
            }
        }];
        self.index  = 0;
        //        [LVHUD showLoding:[BNDPushVCHandler topViewController].view];
        [self handleLoadWebMethod];
        
    }
}

- (void)handleLoadWebMethod
{
    if(self.pdfListData.count >_index){
        @weakify(self)
        __block BNDOfficeDocPDFView *pdfView = self.pdfListData[self.index];
        [pdfView setBlock:^(id _Nullable data) {
            @strongify(self)
            self.index ++;
            [self handleLoadWebMethod];
        }];
        
    }else{
        [LVHUD dismissLoding:[BNDPushVCHandler topViewController].view];
    }
}

- (void)reload
{
    [self.pdfListData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BNDOfficeDocPDFView *pdfView = obj;
        [pdfView reload];
    }];
}

@end
